package com.education.common.utils;

import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Component;

import java.io.*;

/*
文件读写
 */
@Component
public class FileUtil {
    //创建文件夹
    public static boolean CreateFile(String name){
        String path = "education-api/src/main/resources/static/";
        File file = new File(path+name);
        boolean flag = file.mkdir();
        return flag;
    }
    //读取文件
    public static String ReadFile(String path,String name){
        try {

            //从resources中读取数据
//            Resource resource = new ClassPathResource("static/ExamTask/"+name);
//            InputStream is = resource.getInputStream();
//            InputStreamReader isr = new InputStreamReader(is);
            //绝对路径中的文件读取数据
            String file = path+name;
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file),"UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String data = null;
            while ((data = br.readLine()) != null) {
                System.out.println(data);
                return data;
            }
            br.close();
            isr.close();
//            is.close();
        } catch (IOException e){
            e.printStackTrace();
        }
        return "错误";
    }
    //写入文件
    public static void WriteFile(String name,String data){
        try {
            FileOutputStream fos = new FileOutputStream("education-api/src/main/resources/static/ExamTask/"+name);
            OutputStreamWriter opw = new OutputStreamWriter(fos,"utf-8");
            opw.write(data);

            opw.close();
            fos.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //转化为json
    public static String tojson(Object object){
        Object obj = JSONArray.toJSON(object);
        String json = obj.toString();
        return  json;
    }
    /**
     * 递归删除目录下的所有文件及子目录下所有文件
     * @param dir 将要删除的文件目录
     * @return boolean Returns "true" if all deletions were successful.
     *   If a deletion fails, the method stops attempting to
     *   delete and returns "false".
     */
    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            //递归删除目录中的子目录下
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
    }

    /**
     * 文件删除
     *
     * @param filePath 文件路径
     * @return false、true
     */
    public static Boolean delete(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
            System.out.println("===========单个文件删除成功=================");
            return true;
        } else {
            System.out.println("===============单个文件删除失败==============");
            return false;
        }
    }

}
